'''
Descripttion: 每道题最后必须换行表示结束
version: 
Author: Zhang Lingxin
Date: 2023-06-01 11:48:31
LastEditors: Zhang Lingxin
LastEditTime: 2023-06-01 14:03:18
'''

import re
import json

def recogniQues(filename):
    text = ""
    with open(filename, 'r',encoding='utf-8') as f:
        text = f.read()
    # print(text)
    # print()

    # 选择题正则表达式
    choice_regex = r'(\d+)\.\s+(.*?)\((\w)\)\nA\.\s(.*?)\nB\.\s(.*?)\nC\.\s(.*?)\nD\.\s(.*?)\n'

    # 填空题正则表达式
    fill_regex = r"(\d+)\. (.*?)_(.*?)_(.*?)\n"

    # 判断题正则表达式
    judge_regex = r'(\d+)\.\s+(.*?)\((T|F)\)'

    # 使用正则表达式提取题号、题干、分析和答案
    text_regex = r'(\d+)\. (.*?)\n::(.*?)\n:::(.*?)\n'

    ques_choice = {
        'question': '',
        'question_A': '',
        'question_B': '',
        'question_C': '',
        'question_D': '',
        'right_answer': ''
    }

    ques_fill = {
        'question': '',
        'right_answer': ''
    }

    ques_judge = {
        'question': '',
        'right_answer': ''
    }

    ques_text = {
        'question': '',
        'analysis': '',
        'right_answer': ''
    }

    # 识别选择题
    choice_matches = re.findall(choice_regex, text)
    for match in choice_matches:
        ques_choice['question'] = match[1]
        ques_choice['question_A'] = match[3]
        ques_choice['question_B'] = match[4]
        ques_choice['question_C'] = match[5]
        ques_choice['question_D'] = match[6]
        ques_choice['right_answer'] = match[2]
        # print("题号 ", match[0])
        # print("题干 ", match[1])
        # print("选项A ", match[3])
        # print("选项B ", match[4])
        # print("选项C ", match[5])
        # print("选项D ", match[6])
        # print("答案 ", match[2])
        # print()
    ques_choice_json = json.dumps(ques_choice, ensure_ascii=False)

    # 识别填空题
    fill_matches = re.findall(fill_regex, text)
    for match in fill_matches:
        ques_fill['question'] = match[1]
        ques_fill['right_answer'] = match[2]
        # print("题号 ", match[0])
        # print("题干 ", match[1])
        # print("答案 ", match[2])
        # print()
    ques_fill_json = json.dumps(ques_fill, ensure_ascii=False)

    # 识别判断题
    judge_matches = re.findall(judge_regex, text)
    for match in judge_matches:
        ques_judge['question'] = match[1]
        ques_judge['right_answer'] = match[2]
        # print("题号 ", match[0])
        # print("题干 ", match[1])
        # print("答案 ", match[2])
        # print()
    ques_judge_json = json.dumps(ques_judge, ensure_ascii=False)

    # 简答题
    text_matches = re.findall(text_regex, text)
    for match in text_matches:
        ques_text['question'] = match[1]
        ques_text['analysis'] = match[2]
        ques_text['right_answer'] = match[3]
        # print("题号 ", match[0])
        # print("题干 ", match[1])
        # print("分析 ", match[2])
        # print("答案 ", match[3])
        # print()
    ques_text_json = json.dumps(ques_text, ensure_ascii=False)

    # 结果
    print(ques_choice_json)
    print(ques_fill_json)
    print(ques_judge_json)
    print(ques_text_json)

# recogniQues('test.txt')